import React from 'react'
import { Button, Checkbox, Form, Input } from 'antd';
import axios from '../../api/api'
import './index.css'
import { cctoken,ccusername } from '../../store/slice/twice';
import { useNavigate } from 'react-router-dom';
import {
    HomeOutlined,
    LoadingOutlined,
    SettingFilled,
    SmileOutlined,
    SyncOutlined,
    LockOutlined,
    UserOutlined
  } from '@ant-design/icons';
import { useDispatch } from 'react-redux';
function Index() {
    const nav=useNavigate()
   const dispatch= useDispatch()
    const onFinish = async(values) => {
        console.log('Success:', values);
        const {account,password}=values
        const res=await axios.post('/api/login',{account,password})
        console.log(res.data)
        localStorage.setItem('token',res.data.token)
        localStorage.setItem('username',res.data.username)
        dispatch(cctoken(res.data.token))
        dispatch(ccusername(res.data.username))
        alert('登录成功')
        nav('/home')
      };
      const onFinishFailed = errorInfo => {
        console.log('Failed:', errorInfo);
      };
  return (
    <div className='qb'>
        <div className='one1'>
            <h2 style={{marginLeft:'73px'}}>react-ant-admin</h2>
            <p style={{marginLeft:'100px'}}>欢迎使用,请先登录</p>
        <Form
    name="basic"
    labelCol={{ span: 8 }}
    wrapperCol={{ span: 16 }}
    style={{ maxWidth: 600 }}
    initialValues={{ remember: true }}
    onFinish={onFinish}
    onFinishFailed={onFinishFailed}
    autoComplete="off"
  >
    <Form.Item
      label=""
      name="account"
      rules={[{ required: true, message: 'Please input your username!' }]}
    >
      <Input style={{width:'350px',marginTop:'25px'}} prefix={<UserOutlined />}/>
    </Form.Item>

    <Form.Item
      label=""
      name="password"
      rules={[{ required: true, message: 'Please input your password!' },
        {pattern:/^\w{6,12}$/,message: '请输入6 - 12 位数字和字母'}
      ]}
    >
      <Input.Password style={{width:'350px'}}   prefix={<LockOutlined />}/>
    </Form.Item>

    <Form.Item name="remember" valuePropName="checked" label={null}>
      <Checkbox style={{float:'left',marginLeft:'-110px'}}>记住我</Checkbox>
    </Form.Item>

    <Form.Item label={null}>
      <Button type="primary" htmlType="submit" style={{float:'left',marginLeft:'-60px'}}>
        登录
      </Button>
      <Button type="primary" htmlType="submit" style={{float:'left',marginLeft:'85px',background:'white',color:'black'}}>
        重置
      </Button>
    </Form.Item>
  </Form>
    </div>
    </div>
  )
}

export default Index
